#!/bin/bash
# Pega a url de um canal do youtube do clipboard (usando xclip),
# ou a partir do $1, se presente, e te dá o feed RSS para aquele
# canal na área de transferência.

if [[ $1 == '' ]]
then
	start=$(xclip -selection clipboard -o)
	channel=$(curl -s $start | head -n 2500 | egrep -o data-channel-external-id=\"........................\" | head -1)
	channelid=$(echo $channel | sed s/data-channel-external-id=\"// | sed s/\"//)
	prefix=$(echo "https://www.youtube.com/feeds/videos.xml?channel_id=")
	echo $prefix$channelid | xclip -selection clipboard -i
else
	channel=$(curl -s $1 | head -n 2500 | egrep -o data-channel-external-id=\"........................\" | head -1)
	channelid=$(echo $channel | sed s/data-channel-external-id=\"// | sed s/\"//)
	prefix=$(echo "https://www.youtube.com/feeds/videos.xml?channel_id=")
	#echo $prefix$channelid
	echo $prefix$channelid | xclip -selection clipboard -i
fi
